package com.audaque.springboot.foshanupload.web.springsecuritycas.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * @author zgb
 * @desc 认证成功处理器
 * @date 2022-08-05 18:00:12
 */
@Slf4j
public class UrlAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    public UrlAuthenticationSuccessHandler() {
        super();
    }


    public UrlAuthenticationSuccessHandler(String defaultTargetUrl) {
        super(defaultTargetUrl);
    }

    /**
     * 认证成功后
     *
     * @param request
     * @param response
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {


        super.onAuthenticationSuccess(request, response, authentication);
    }

    /**
     * 返回一个认证成功后的路径
     *
     * @param request
     * @param response
     * @return
     */

    @Override
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {

        //根据自己需求写
        HttpSession session = request.getSession();
        if(session!=null){
            String targetUrl = (String) session.getAttribute("REQUEST_URL");
            log.debug("目标路径为{}", targetUrl);
            return targetUrl.toString();
        }
        return null;
    }
}

